
#include "TwTerrainMgr.h "
#include "TwTerrain.h"
#include "../TwEngineDx.h"
#include "TwGrass.h"
#include "../image/TwGenerateImage.h"


void TwTerrainMgr::Init()
{
	m_pkTerrain = new TwTerrain();
	m_pkTerrain->Init( 256.0f, 4, 250.f, g_Render->GetDevice() );

	m_pkGrass = new TwGrass();
	m_pkGrass->InitGrass();
	InitRenderState();

	//m_pkImage = new TwGenerateImage();
	//m_pkTerrain->SetTerrainTexTure(m_pkImage->GenerateNewImage(0,0,256,256),0);
}

void TwTerrainMgr::Update()
{
}

void TwTerrainMgr::Render()
{
	m_pkTerrain->Render();
	InitRenderState();
	m_pkGrass->Render();
}

void TwTerrainMgr::Clear()
{
}
void TwTerrainMgr::InitRenderState()
{
 
	g_Render->GetDevice()->SetRenderState(D3DRS_ZENABLE, TRUE);
	g_Render->GetDevice()->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);   

	g_Render->GetDevice()->SetRenderState(D3DRS_ALPHATESTENABLE, TRUE);    // turn on the color blending
	g_Render->GetDevice()->SetRenderState(D3DRS_ALPHAREF, 0x80);
	g_Render->GetDevice()->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL);

	g_Render->GetDevice()->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);    // turn on the color blending

	g_Render->GetDevice()->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);    // set source factor
	g_Render->GetDevice()->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);    // set dest factor


	g_Render->GetDevice()->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
	g_Render->GetDevice()->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
	g_Render->GetDevice()->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
	g_Render->GetDevice()->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
	g_Render->GetDevice()->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
	g_Render->GetDevice()->SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);
}
